package org.stream;

import com.fasterxml.jackson.databind.util.ArrayBuilders;
import org.entity.Employee;
import org.junit.jupiter.api.Test;

import java.math.BigInteger;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Description
 * @Author wjj
 * @Date 2023/4/17 22:11
 */
public class Map {
    /*map操作对每个元素应用一个函数以生成另一个流。
    输入流和输出流中的元素数量是相同的。
    该操作不会修改输入流的元素。*/

    //    IntStream map(IntUnaryOperator mapper)
    //    DoubleStream mapToDouble(IntToDoubleFunction mapper)
    //    LongStream mapToLong(IntToLongFunction mapper)
    //    <U> Stream<U> mapToObj(IntFunction<? extends  U> mapper)
    @Test
    public void mapTest(){
        //下面的代码展示了如何使用map()将元素从IntStream映射到它们的方块，并在标准输出中打印映射的流。
        IntStream.rangeClosed(1, 5)
                .map(n->n*n)
                .forEach(System.out::println);
        System.out.println("----------------------");

        Employee.persons().stream()
                .map(Employee::getName)
                .forEach(System.out::println);
    }

    @Test
    public void flatMapTest(){
        Stream.of(1,2,3)
                .flatMap(n->Stream.of(n,n+1))
                .forEach(System.out::println);
    }

    @Test
    public void mapToObjTest(){
        Stream.of("XML", "Java",  "CSS")
                .map(name->name.chars())
                .flatMap(intStream -> intStream.mapToObj(n->(char)n))
                .forEach(System.out::println);
    }

    @Test
    public void test(){
        Stream.of("XML", "Java",  "CSS")
                .flatMap(name ->  IntStream.range(0, name.length())
                        .mapToObj(name::charAt))
                .forEach(System.out::println);
    }

    @Test
    public void test1(){
        String replace = UUID.randomUUID().toString();
        System.out.println(replace);
        BigInteger.probablePrime(3,new Random());
        String.valueOf(1);
        System.out.println(BigInteger.valueOf(Integer.MAX_VALUE));
        java.util.Date date = Date.from(Instant.now());
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
        System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()));
        long timeMillis = System.currentTimeMillis();
        System.out.println(new SimpleDateFormat().format(new java.util.Date(timeMillis)));

    }

    @Test
    public void sum(){
        System.out.println(new SimpleDateFormat("HH:mm:ss").format(new java.util.Date(System.currentTimeMillis())));
        long sum = 0L;

        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            sum+=1;
        }
        System.out.println(new SimpleDateFormat("HH:mm:ss").format(new java.util.Date(System.currentTimeMillis())));
    }

    @Test
    public int[] twoSum(){
        int[] nums = {3,2,4}; int target = 6;
        HashMap<Integer, Integer> map = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])){
                return new int[]{map.get((target - nums[i])),i};
            }
            map.put(nums[i],i);
        }
        return new int[]{0,0};
    }

    @Test
    public void lengthOfLongestSubstring(){
       String s = "abcc";
        int length = s.length();
        int num = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int start = 0,end = 0; end < length; end++) {
            char c = s.charAt(end);
            if (map.containsKey(c) && map.get(c)>=start){
                start = map.get(c)+1;
            }else {
                num = Math.max(num, end-start+1);
            }
            map.put(c,end);
        }
        System.out.println(num);
    }

    @Test
    public void  rome(){
        int x = 1211;
        if (x<=0 || x%10==0){
            System.out.println("false");
        }
        int reNum= 0;
        while (x>reNum){
            reNum = reNum*10 + x%10;
            x/=10;
        }
        System.out.println(reNum==x || reNum/10==x);
    }

    @Test
    public void longestPalindrome(){
        String s = "babad";
        String str = "";
        HashMap<Character, Character>  map = new LinkedHashMap<>();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i),s.charAt(i));
            if (map.containsKey(s.charAt(i))){
                String string = map.values().toString();
                System.out.println(string);
                str = str.length()>string.length()?str:string;
            }
        }
    }

    @Test
    public void findErrorNums(){

        int[] nums = {3,2,2};
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i < nums.length+1; i++) {
            arrayList.add(i);
        }
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
           if (nums[i]-arrayList.get(i)==1){
               System.out.println(nums[i]+"-"+(nums[i]-1));
               break;
           }
           if (nums[i]-arrayList.get(i)==-1){
               System.out.println(nums[i]+"-"+(nums[i]+1));
               break;
           }

        }
    }

    @Test
    public int reverse() {
        int x = -214783648;
        Long y =0L;
        while (x!=0){
            y = y*10 + x%10;
            x = x/10;
        }
        if (y < Integer.MIN_VALUE || y > Integer.MAX_VALUE) {
            return 0;
        }else {
            return Math.toIntExact(y);
        }

    }
}